package 力扣刷题.探索初级算法.数组.移动零;

import java.util.Scanner;

public class Solution {
    public void moveZeroes(int[] nums) {

        if(nums.length==0||nums.length==1)
            return;
        else {
            int zeroStartPointer=0;
           zeroStartPointer= findNextZeroStartPosition(nums, zeroStartPointer);
            for (int i = zeroStartPointer; i < nums.length; i++) {
                if(nums[i]!=0) {
                    swap(nums, i, zeroStartPointer);
                    zeroStartPointer= findNextZeroStartPosition(nums, zeroStartPointer);
                }
            }
        }
    }
    public void swap(int[] nums,int firstPos,int secondPos) {
        int temp=nums[firstPos];
        nums[firstPos]=nums[secondPos];
        nums[secondPos]=temp;
    }
    public int findNextZeroStartPosition(int[] nums,int zeroStartPointer) {
        for (int i = zeroStartPointer; i < nums.length; i++) {
            if(nums[i]==0){
                return i;
            }
        }
        return nums.length;//特殊情况
    }
    public static void main(String[] args) {
        int len;
        Scanner s=new Scanner(System.in);
        len=s.nextInt();
        int[] nums=new int[len];
        for (int i = 0; i < len; i++) {
            nums[i]=s.nextInt();
        }
        new Solution().moveZeroes(nums);
        for (int i = 0; i < len; i++) {
            System.out.println(nums[i]);
        }
    }
}
